/*
 * 管理我的账号
 * page管理我的账号
 */
import 'package:flutter/material.dart';
import 'package:qgchat/bloc/my/manageAccount.dart';
import 'package:qgchat/common/global/global.dart';
import 'package:qgchat/states/states_model/index.dart';

class ManageAccount extends StatefulWidget {
  ManageAccount({Key key}) : super(key: key);

  @override
  _ManageAccountState createState() => _ManageAccountState();
}

class _ManageAccountState extends State<ManageAccount> {
  /// 管理账号bloc
  ManageAccountBloc bloc = ManageAccountBloc();

  /// 用户支付宝账号
  TextEditingController alipayAccount = TextEditingController();

  /// 用户支付宝姓名
  TextEditingController alipayMember = TextEditingController();

  @override
  void didChangeDependencies() {
    if (mounted) {
      UserState userState = (state(context, 'user') as UserState);

      if (userState.alipay.alipayUser != null) {
        alipayAccount
          ..text = userState.alipay.alipayUser
          ..selection = TextSelection.collapsed(
            offset: userState.alipay.alipayUser.runes.length,
          );
        alipayMember
          ..text = userState.alipay.alipayName
          ..selection = TextSelection.collapsed(
            offset: userState.alipay.alipayName.runes.length,
          );
      }
    }
    super.didChangeDependencies();
  }

  @override
  void dispose() { 
    bloc.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: PreferredSize(
        preferredSize: Size.fromHeight(px(88)),
        child: AppBar(
          centerTitle: true,
          title: Text(
            "管理我的账号",
            style: TextStyle(
              color: Colors.black,
              fontSize: px(34),
              fontWeight: FontWeight.bold,
            ),
          ),
          actions: <Widget>[
            Container(
              width: px(130),
              child: FlatButton(
                onPressed: () {
                  UserState userState = (state(context, 'user') as UserState);
                  bloc.comfiralipay(
                    userState.alipay.id,
                    userState.info.memberToken,
                    alipayAccount.text,
                    alipayMember.text,
                    () {
                      userState.alipayInfo();
                    },
                  );
                },
                padding: EdgeInsets.zero,
                child: Text(
                  "保存",
                  style: TextStyle(
                    color: col('5640c6'),
                    fontSize: px(34),
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
      body: Container(
        height: flex1,
        color: Colors.white,
        child: SingleChildScrollView(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              // 头部渐变层
              _hand,
              // 支付宝
              _title('支付宝'),
              // 账号
              _subTitle('支付宝账号'),
              // 输入框
              _textFiled(alipayAccount),
              // 密码
              _subTitle('支付宝姓名'),
              // 输入框
              _textFiled(alipayMember),
            ],
          ),
        ),
      ),
    );
  }

  // 头部渐变层
  final Widget _hand = Container(
    width: flex1,
    height: px(196),
    alignment: Alignment.bottomCenter,
    decoration: BoxDecoration(
      gradient: LinearGradient(colors: [
        col("8d2ef5"),
        col("eb6541"),
      ]),
    ),
    child: Container(
      width: flex1,
      height: px(78),
      alignment: Alignment.centerLeft,
      color: Colors.white.withOpacity(.1),
      padding: EdgeInsets.symmetric(horizontal: px(30)),
      child: Text(
        "请认真填写您的账户信息，这将直接关系到您的收款",
        style: TextStyle(
          color: Colors.white,
          fontSize: px(24),
        ),
      ),
    ),
  );

  // 账号title
  Widget _title(text) {
    return Container(
      margin: EdgeInsets.only(top: px(50)),
      padding: EdgeInsets.symmetric(horizontal: px(30)),
      child: Text(
        '$text',
        style: TextStyle(
          color: col('303133'),
          fontSize: px(44),
          fontWeight: FontWeight.bold,
        ),
      ),
    );
  }

  // 账号subtitle
  Widget _subTitle(text) {
    return Container(
      margin: EdgeInsets.only(top: px(60)),
      padding: EdgeInsets.symmetric(horizontal: px(30)),
      child: Text(
        '$text',
        style: TextStyle(
          color: col('909399'),
          fontSize: px(26),
          fontWeight: FontWeight.bold,
        ),
      ),
    );
  }

  // 输入框
  Widget _textFiled(textController) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: px(30)),
      child: TextField(
        controller: textController,
      ),
    );
  }
}
